/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/jss/jss.xml", "tibco.admin", "jss_25H", "ljss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "SSL_SERVER";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_SSLServer");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@ServerSSL Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefDetails(master, parent, null,
                        null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface

        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONSHOWDETAILS = "tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ONSHOWDETAILS";
            details.ONHIDEDETAILS = "tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ONHIDEDETAILS";
            details.prototype.subType = null;
            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.SSLServer";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["selIdentity_25H", "selCipherStrength_25H", "radClientAuthRequiredYes_25H",
                        "radClientAuthRequiredNo_25H", "txtStoreURL_25H", "txtPassword_25H"], this.editableComponentList.size());
                this.rwComponentList.addAll(["selIdentity_25H", "selCipherStrength_25H", "blkClientAuthRequiredYesNo_25H", "txtStoreURL_25H", "txtPassword_25H"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcIdentity_25H", "lblcCipherStrength_25H", "lblcClientAuthRequired_25H", "lblcStoreURL_25H", "lblcPassword_25H"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_SSLServer");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getSSLServerResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/rules/getSSLServerResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editSSLServerResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/rules/editSSLServerResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addSSLServerResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/rules/addSSLServerResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/components/shdSSLServerResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return null;
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_SSLServer");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_SSLServer");
            }

            details.prototype.initializeGenericDetails = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="NO_RESTRICTION" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_RESTRICTION_25H") + '" />';
                    str += '<record jsxid="NO_EXPORT_GRADE" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_EXPORT_GRADE_25H") + '" />';
                    str += '<record jsxid="AT_LEAST_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_128_BIT_25H") + '" />';
                    str += '<record jsxid="MORE_THAN_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@MORE_THAN_128_BIT_25H") + '" />';
                    str += '<record jsxid="AT_LEAST_256_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_256_BIT_25H") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selCipherStrength_25H");
                    tibco.admin.Main.getServer();
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").subscribe(details.ONSHOWDETAILS, this, this.showDetails);
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredNo_25H").subscribe(details.ONHIDEDETAILS, this, this.hideDetails);
                }
                this.jsxsuper(objEvent);

				objEvent.obj.handler = this;//070818
            }

            details.prototype.initializeGenericAdd = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="NO_RESTRICTION" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_RESTRICTION_25H") + '" />';
                    str += '<record jsxid="NO_EXPORT_GRADE" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_EXPORT_GRADE_25H") + '" />';
                    str += '<record jsxid="AT_LEAST_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_128_BIT_25H") + '" />';
                    str += '<record jsxid="MORE_THAN_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@MORE_THAN_128_BIT_25H") + '" />';
                    str += '<record jsxid="AT_LEAST_256_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_256_BIT_25H") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selCipherStrength_25H");
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").subscribe(details.ONSHOWDETAILS, this, this.showDetails);
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredNo_25H").subscribe(details.ONHIDEDETAILS, this, this.hideDetails);
                }
                this.jsxsuper(objEvent);
                this.callgetResourceConfigurationsInEnterprise();

				objEvent.obj.handler = this;//070818
				this.initializeSSLParam(objEvent);//070818
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                this.callgetResourceConfigurationsInEnterprise();
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetSSLServerDetailsResponse_25H").getRecordNode("jsxroot");
                this.jsxsuper(response);

                var value = response.getAttribute("identity");
                if(value == "" || value == null){
                    this.mainComponent.getDescendantOfName("selIdentity_25H").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@selIdentity_25H")); 
                }else{
                    this.mainComponent.getDescendantOfName("selIdentity_25H").setValue(value, true);
                }
                this.mainComponent.getDescendantOfName("lblcIdentity_25H").setText(value, true);

                value = response.getAttribute("ciperStrength");
                if(value == "" || value == null){
                    this.mainComponent.getDescendantOfName("selCipherStrength_25H").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@CipherStrength_25H")); 
                }else{
                    this.mainComponent.getDescendantOfName("selCipherStrength_25H").setValue(value, true);
                }
                this.mainComponent.getDescendantOfName("lblcCipherStrength_25H").setText(this.mainComponent.getDescendantOfName("selCipherStrength_25H").getText(), true);
                this.mainComponent.getDescendantOfName("blkCipherStrength_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                value = response.getAttribute("clientAuthenticationRequired");
                this.mainComponent.getDescendantOfName("lblcClientAuthRequired_25H").setText((value == "true") ? tibco.admin.Main.getServer().getDynamicProperty("@@radClientAuthRequiredYes_25H") : tibco.admin.Main.getServer().getDynamicProperty("@@radClientAuthRequiredNo_25H"), true);
                this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").setGroupValue(value, true);
                if(value == "true") {
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").setSelected(jsx3.gui.RadioButton.SELECTED);
                    this.mainComponent.getDescendantOfName("blkTrustCetificateAuth_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.mainComponent.getDescendantOfName("blkStoreType_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    value = response.getAttribute("certStoreURL");
                    this.mainComponent.getDescendantOfName("lblcStoreURL_25H").setText(value, true);
                    this.mainComponent.getDescendantOfName("txtStoreURL_25H").setValue(value, true);
                    this.mainComponent.getDescendantOfName("blkStoreURL_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    value = response.getAttribute("password");
                    this.mainComponent.getDescendantOfName("lblcPassword_25H").setText("******", true);
                    this.mainComponent.getDescendantOfName("txtPassword_25H").setValue(value, true);
                    this.mainComponent.getDescendantOfName("blkPassword_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                }
                else {
                    this.mainComponent.getDescendantOfName("radClientAuthRequiredNo_25H").setSelected(jsx3.gui.RadioButton.SELECTED);
                }

                this.setEditable(true);
                this.initializeSSLParam(objEvent);
            }

            details.prototype.showDetails = function(event) {
                this.mainComponent.getDescendantOfName("blkTrustCetificateAuth_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.mainComponent.getDescendantOfName("blkStoreType_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.mainComponent.getDescendantOfName("blkStoreURL_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.mainComponent.getDescendantOfName("blkPassword_25H").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
				this.initializeSSLParamCAR(event);
            }

            details.prototype.hideDetails = function(event) {
                this.mainComponent.getDescendantOfName("blkTrustCetificateAuth_25H").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                this.mainComponent.getDescendantOfName("blkStoreType_25H").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                this.mainComponent.getDescendantOfName("blkStoreURL_25H").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                this.mainComponent.getDescendantOfName("blkPassword_25H").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
				this.initializeSSLParam(event);
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.identity = this.mainComponent.getDescendantOfName("selIdentity_25H").getValue();
                objService.ciperStrength = this.mainComponent.getDescendantOfName("selCipherStrength_25H").getValue();
                objService.clientAuthenticationRequired = this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").getGroupValue();
                if(this.mainComponent.getDescendantOfName("radClientAuthRequiredYes_25H").getGroupValue() == "true") {
                    objService.storeType = "JKS";
                    objService.certStoreURL = this.mainComponent.getDescendantOfName("txtStoreURL_25H").getValue();
                    objService.password = this.mainComponent.getDescendantOfName("txtPassword_25H").getValue();
                }
            }

            //call this method to begin the service call (eg.service.callgetResourceConfigurationsInEnterprise();)
            details.prototype.callgetResourceConfigurationsInEnterprise = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/rules/getPKI_IDENTITYResourceConfigurationsInEnterprise.xml",
                        "getPKIIdentityResourceConfigInEnterprise", "Get getPKI_IDENTITY Shared Resources Configurations")
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetResourceConfigurationsInEnterpriseSuccess);
                objService.doCall();
            };

            details.prototype.ongetResourceConfigurationsInEnterpriseSuccess = function(objEvent) {
                this.mainComponent.getDescendantOfName("selIdentity_25H").repaint();
            };


			/*070818
			* if user doesn't fishish all the required fields, the "save" button will be disabled.  part 2 for SSL Server
			*/
			details.prototype.valiSSLIdentity = function(obj) {
                this.passedItems["SSLIdentity"] = tibco.admin.util.Validation.doValidateSelectBox(obj, tibco.admin.Main.getServer().getDynamicProperty("@@identity_25H"));
				this.enableSaveBtn4Add();
            }

            details.prototype.valiCSURL = function(obj, strValue) {
                this.passedItems["CSURL"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@certStoreURL_25H"));
				this.enableSaveBtn4Add();
            }

            details.prototype.valiCSPWD = function(obj, strValue) {
                this.passedItems["CSPWD"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@password_25H"));
				this.enableSaveBtn4Add();
            }

			details.prototype.initializeSSLParam = function(objEvent) {
				this.passedItems = {};//070818
				this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;//070818
				var objSelIdentity = this.mainComponent.getDescendantOfName("selIdentity_25H");
				this.passedItems["SSLIdentity"] = (objSelIdentity.getValue() && (objSelIdentity.getValue()!="- Select -"))?true:false;//070818
				this.enableSaveBtn4Add();//070818
			}
			details.prototype.initializeSSLParamCAR = function(objEvent) {
				this.passedItems = {};//070818
				this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;//070818
				var objSelIdentity = this.mainComponent.getDescendantOfName("selIdentity_25H");
				this.passedItems["SSLIdentity"] = (objSelIdentity.getValue() && (objSelIdentity.getValue()!="- Select -"))?true:false;//070818
				var objStoreURL = this.mainComponent.getDescendantOfName("txtStoreURL_25H");
                this.passedItems["CSURL"] = tibco.admin.util.Validation.doValidateNotEmpty(objStoreURL, null, tibco.admin.Main.getServer().getDynamicProperty("@@certStoreURL_25H"));
				var objStorePWD = this.mainComponent.getDescendantOfName("txtPassword_25H");
				this.passedItems["CSPWD"] = tibco.admin.util.Validation.trimAll(objStorePWD.getValue())?true:false;
				this.enableSaveBtn4Add();//070818
			}

        });


